<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends CommonController {
    public function index()
    {
    	
        $admin=D('Admin');
        $where=array();
        if($kw=I('kw')){
            $where['name'] = array('like','%'.$kw.'%');
        }
        $count=$admin->where($where)->count();// 查询满足要求的总记录数
        $Page=new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show= $Page->show();// 分页显示输出
        $list = $admin->field('a.id,a.name,b.rolename')->alias('a')->join('LEFT JOIN role b ON a.roleid=b.id')->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出

        $this->display();
    }

    public function add(){
    	 $admin=D('Admin');
    	//var_dump($_POST);exit;
        if($_POST){
            if($admin->create()){
                $admin->password=md5($admin->password);
                if($admin->add()){
                    $this -> success('添加成功',U('Admin/index'));
                }else{
                    $this -> error('添加失败');
                }
            }else{
                $this -> error($admin->getError());
                //exit($category->getError());
            }
            return;
        }
        $data=D('Role')->select();
        $this->assign("data",$data);
    	$this->display();
    }


public function edit(){
	 $admin=D('Admin');
        if($_POST){
            if($admin->create()){
                $admin->password=md5($admin->password);
                if($admin->save()){
                    $this -> success('修改成功',U('Admin/index'));
                }else{
                    $this -> error('修改失败');
                }
            }else{
                $this -> error($admin->getError());
                //exit($category->getError());
            }
            return;
        }
        $adminid=I('get.id');
        $info=$admin->find($adminid);
        $this->assign('info',$info);
        $data=D('Role')->select();
        $this->assign("data",$data);
    	$this->display();

}

 public function del()
    {
        $admin=D('Admin');
        $id=I('id');
        if($admin->delete($id))
        {
            $this->success('成功删除管理员！',U('Admin/index'));
        }else
        {
            $this->error('删除管理员失败！');
        }
    }

    public function batchDel()
    {
        $admin=D('Admin');
        $ids=I('post.ids');
        $ids=implode(',', $ids);
        if($ids)
        {
            if($admin->delete($ids))
            {
                $this->success('批量删除管理员成功！',U('Admin/index'));
            }else
            {
                $this->error('批量删除管理员失败！');
            }
        }else
        {
            $this->error('未选中任何内容！');
        }
    }



    public function logout(){
    session(null);
    $this->success('退出成功！',U('Login/index'));
   }
}
